<?php
class Tour_model extends Model {

    function Tour_model()
    {
        // Call the Model constructor
        parent::Model();
    }
	
	function get_all_tours()
	{
		$this->db->select('tours.*, ' . $this->db->dbprefix . 'tourtypes.tour_name, ' . $this->db->dbprefix . 'tourtypes.tour_code');
		$query = $this->db->where('tourtypes.id = ' . $this->db->dbprefix . 'tours.tourtype_id');
		$query = $this->db->order_by('departure');
		$query = $this->db->get('tours, ' . $this->db->dbprefix . 'tourtypes');
		
		$tours = $query->result_array();
		
		return $tours;	
	}
	
	function get_tours_for_booking($params, $showFull = true)
	{
		$this->db->select('tours.*, ' . $this->db->dbprefix . 'tourtypes.tour_name, ' . $this->db->dbprefix . 'tourtypes.tour_code');
		$query = $this->db->where('tourtypes.id = ' . $this->db->dbprefix . 'tours.tourtype_id');
		
		if ($params['tourtype'] > 0)
		{
			$query = $this->db->where('tourtypes.id = ' . $params['tourtype']);
		}
		
		if (!$showFull)
		{
			
		}	
		
		
		
		//$query = $this->db->where('departure >= \'' . date("Y-m-d") . '\'');
		$query = $this->db->where('active', '1');
		$query = $this->db->order_by('departure');
		$query = $this->db->get('tours, ' . $this->db->dbprefix . 'tourtypes');
		
		$tours = $query->result_array();
		
		return $tours;	
	}
	
	function get_tours_by_month($month, $year = '2009', $active = true)
	{
		
		
		
		
		$this->db->select('tours.*, ' . $this->db->dbprefix . 'tourtypes.tour_name, ' . $this->db->dbprefix . 'tourtypes.tour_code, ' . $this->db->dbprefix . 'tourtypes.tour_nights, ' . $this->db->dbprefix . 'tourtypes.calendar_color');
		$this->db->where('MONTH(`departure`)', $month);
		$this->db->where('YEAR(`departure`)', $year);
		$this->db->where('active', 1);
		$this->db->where('tourtypes.id = ' . $this->db->dbprefix . 'tours.tourtype_id');
		$this->db->order_by('departure');
		$this->db->order_by('tours.id');
		$query = $this->db->get('tours, ' . $this->db->dbprefix . 'tourtypes');
		
		$tours = $query->result_array();
		
		return $tours;
		
	}
	
	
    function add_tour()
    {
		$data = array(
	               	'tourtype_id' => $_POST['tourtype'],
					'departure' => date_for_db($_POST['departure_date'])
		            );

		$this->db->insert('tours', $data);
		
		return $this->db->insert_id();
    }
	
	function get_tour($id)
	{
		$this->db->select('tours.*, ' . $this->db->dbprefix . 'tourtypes.tour_name, ' . $this->db->dbprefix . 'tourtypes.tour_nights, ' . $this->db->dbprefix . 'tourtypes.default_price');
		$query = $this->db->where(array('tours.id' => $id));
		$query = $this->db->where('tourtypes.id = ' . $this->db->dbprefix . 'tours.tourtype_id');
		$query = $this->db->get('tours, ' . $this->db->dbprefix . 'tourtypes');
		
		$tour = $query->result_array();
		
		return $tour[0];
	}
	
	function update($id)
	{
		$data = array(
	               	'tourtype_id' => $_POST['tourtype'],
					'departure' => date_for_db($_POST['departure_date'])
		            );
		
		if (isset($_POST['special_offer']))
		{
			$data['special_offer'] = 1;
		} else
		{
			$data['special_offer'] = 0;
		}
		
		if (!isset($_POST['use_default_price']) && (strlen(trim($_POST['price'])) > 0 && $_POST['price'] > 0))
		{
			$data['use_default_price'] = 0;
			$data['custom_price'] = $_POST['price'];
		} else
		{
			$data['use_default_price'] = 1;
			$data['custom_price'] = 0;
			$data['special_offer'] = 0;
		}
		
		$this->db->where('id', $id);
		$this->db->update('tours', $data);
		
		$this->session->set_flashdata('msg', 'Tour successfully updated');
		
		return true;
	}
	
	function updateCapacity($id)
	{

		$this->db->select('tour_to_bus.*, ' . $this->db->dbprefix . 'buses.capacity');
		$query = $this->db->where(array('tour_to_bus.tour_id' => $id));
		$query = $this->db->where('buses.id = ' . $this->db->dbprefix . 'tour_to_bus.bus_id');
		$query = $this->db->get('tour_to_bus, ' . $this->db->dbprefix . 'buses');
		
		$buses = $query->result_array();
		
		$c = 0;
		
		for ($i = 0; $i < count($buses); $i++)
		{
			$c += $buses[$i]['capacity'];
		}
		
		$data = array('capacity' => $c);
		
		$this->db->where('id', $id);
		$this->db->update('tours', $data);
		
	}
	
	function activate($id)
	{
		$data = array();
		$data['active'] = 1;
		$this->session->set_flashdata('msg', 'Tour activated');
		
		$this->db->where('id', $id);
		$this->db->update('tours', $data);
		
		return true;
	}
	
	function deactivate($id)
	{
		$data = array();
		$data['active'] = 0;
		$this->session->set_flashdata('msg', 'Tour deactivated');
		
		$this->db->where('id', $id);
		$this->db->update('tours', $data);
		
		return true;
	}
	
	function check_buses($id)
	{
		$query = $this->db->where('tour_id', $id);
		$query = $this->db->get('tour_to_bus');
		
		$buses = $query->result_array();
		
		$proceed = true;
		
		if (count($buses) < 1)
		{
			return false;
		}
		
		for ($i = 0; $i < count($buses); $i++)
		{
			if ($buses[$i]['bus_id'] == 0)
			{
				$proceed = false;
			}
		}
		
		return $proceed;
	}
	
	function get_tourcode($tour_id)
	{
		$this->db->select($this->db->dbprefix . 'tourtypes.tour_code');
		$query = $this->db->where('tourtypes.id = ' . $this->db->dbprefix . 'tours.tourtype_id');
		$query = $this->db->where($this->db->dbprefix . 'tours.id = ' . $tour_id);
		$query = $this->db->get($this->db->dbprefix . 'tours, ' . $this->db->dbprefix . 'tourtypes');		
		$tour = $query->result_array();
		return $tour[0]['tour_code'];
	}

		
	function get_departure($tour_id)
	{
		$string = 'SELECT DATE_FORMAT('. $this->db->dbprefix . 'tours.departure,\'%Y%m%d\') as departure' ;
		$string .= ' FROM '. $this->db->dbprefix . 'tours';		
		$string .= ' WHERE '. $this->db->dbprefix . 'tours.id = ' . $tour_id;
		$query = $this->db->query($string);
		$result = $query->row();
		return $result->departure;
	}
		
	function get_tour_info($tour_id)
	{
		$this->db->select($this->db->dbprefix . 'tourtypes.tour_name, ' . $this->db->dbprefix . 'tours.departure, ' .$this->db->dbprefix . 'tours.custom_price,' . $this->db->dbprefix . 'tourtypes.default_price');
		$query = $this->db->where($this->db->dbprefix .'tourtypes.id = ' . $this->db->dbprefix . 'tours.tourtype_id');
		$query = $this->db->where($this->db->dbprefix . 'tours.id = ' . $tour_id);
		$query = $this->db->get($this->db->dbprefix . 'tours, ' . $this->db->dbprefix . 'tourtypes');		
		$tour = $query->result_array();
		return $tour[0];	
	}
	
	function get_tour_booking($booking_id)
	{
		$this->db->select($this->db->dbprefix . 'tourtypes.tour_name, ' . $this->db->dbprefix . 'tours.departure, ' .$this->db->dbprefix . 'tours.custom_price,' . $this->db->dbprefix . 'tourtypes.default_price');
		$query = $this->db->where($this->db->dbprefix .'tourtypes.id = ' . $this->db->dbprefix . 'tours.tourtype_id');
		$query = $this->db->where($this->db->dbprefix .'tours.id = ' . $this->db->dbprefix . 'bookings.tour_id');		
		$query = $this->db->where($this->db->dbprefix . 'bookings.booking_id = ' . $booking_id);
		$query = $this->db->get($this->db->dbprefix . 'tours, ' . $this->db->dbprefix . 'tourtypes, ' . $this->db->dbprefix . 'bookings');		
		$tour = $query->result_array();
		return $tour[0];	
	}	
	
	
}
?>